﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO;
using System.Drawing;

namespace Agents
{
    public partial class AgentSite : System.Web.UI.MasterPage
    {
        #region Event Handlers

        /// <summary>
        /// Occurs when the page is loaded or being postback, and
        /// enables and disables button in the menu according to the 
        /// agent's status.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            Queries queries = new Queries();

            // Reteiving the agent by his username
            Agents agent = queries.GetAgentByUserName(Page.User.Identity.Name);
            EnableDisableButtons(agent.Status);
        }

        /// <summary>
        /// Occurs when the agent clicks an item in the menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void mnuAgentMenu_OnMenuItemClick(object sender, MenuEventArgs e)
        {
            // Check which command was selected
            switch (e.Item.Value)
            {
                case ("StartShift"):
                {
                    StartShift();
                    break;
                }
                case ("EndShift"):
                {
                    EndShift();
                    break;
                }
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Enable and disable buttons according to the agent's status
        /// </summary>
        private void EnableDisableButtons(string status)
        {
            //if the agent is not on shift- enable buttons that he can use. 
            //and dont allow buttons he can't use
            if (status == "לא במשמרת")
            {
                mnuAgentMenu.FindItem("Shifts/EndShift").Selectable = false;
                mnuAgentMenu.FindItem("NextCustomer/SystemRecommondation").Selectable = false;
                mnuAgentMenu.FindItem("NextCustomer/SelectCustomer").Selectable = false;
                mnuAgentMenu.FindItem("NextCustomer/WatchCustomersOnMap").Selectable = false;
                mnuAgentMenu.FindItem("Shifts/StartShift").Selectable = true;
            }

            //if the agent is not on shift- enable buttons that he can use. 
            //and dont allow buttons he can't use
            else if (status == "במשמרת")
            {
                mnuAgentMenu.FindItem("Shifts/EndShift").Selectable = true;
                mnuAgentMenu.FindItem("NextCustomer/SystemRecommondation").Selectable = true;
                mnuAgentMenu.FindItem("NextCustomer/SelectCustomer").Selectable = true;
                mnuAgentMenu.FindItem("NextCustomer/WatchCustomersOnMap").Selectable = true;
                mnuAgentMenu.FindItem("Shifts/StartShift").Selectable = false;
            }

            // If the agent is with a client, don't allow him to get a new
            // client (only from the page that demands reason)
            else
            {
                mnuAgentMenu.FindItem("Shifts/EndShift").Selectable = true;
                mnuAgentMenu.FindItem("NextCustomer/SystemRecommondation").Selectable = false;
                mnuAgentMenu.FindItem("NextCustomer/SelectCustomer").Selectable = false;
                mnuAgentMenu.FindItem("NextCustomer/WatchCustomersOnMap").Selectable = false;
                mnuAgentMenu.FindItem("Shifts/StartShift").Selectable = false;
            }
        }
        
        /// <summary>
        /// End the agent's shift
        /// </summary>
        private void EndShift()
        {
            //changing agent's status to- not on shift.
            Queries queries = new Queries();
            Agents agent = queries.GetAgentByUserName(Page.User.Identity.Name);
            queries.EndShift(agent);

            EnableDisableButtons(agent.Status);
            Response.Redirect("~/Agent/AgentMenu.aspx");
        }

        /// <summary>
        /// Starts the agent's shift
        /// </summary>
        private void StartShift()
        {
            //changing the agents status to - on shift.
            Queries queries = new Queries();
            Agents agent = queries.GetAgentByUserName(Page.User.Identity.Name);
            queries.StartShift(agent);

            EnableDisableButtons(agent.Status);
            Response.Redirect("~/Agent/AgentMenu.aspx");
        }

        /// <summary>
        /// Color the text of a menu item
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public Color GetItemColor(MenuItemTemplateContainer container)
        {
            MenuItem item = (MenuItem)container.DataItem;

            if (item.Depth == 0)
            {
                return Color.White;
            }

            return item.Selectable ? Color.DarkBlue : Color.DarkGray;
        }

        #endregion
    }
}